import * as Highcharts from 'highcharts';
import Highcharts3D from 'highcharts/highcharts-3d';
export const getOption = (dom, data) => {
  Highcharts3D(Highcharts);
  // 修改3d饼图绘制过程
  const round = Math.round;
  const cos = Math.cos;
  const sin = Math.sin;
  const deg2rad = Math.deg2rad;
  Highcharts.wrap(Highcharts.seriesTypes.pie.prototype, 'translate', function (proceed) {
    proceed.apply(this, [].slice.call(arguments, 1));
    const series = this;
    const chart = series.chart;
    const options = chart.options;
    const seriesOptions = series.options;
    const depth = seriesOptions.depth || 0;
    const options3d = options.chart.options3d;
    const alpha = options3d.alpha;
    const beta = options3d.beta;
    let z = seriesOptions.stacking ? (seriesOptions.stack || 0) * depth : series._i * depth;
    z += depth / 2;
    if (seriesOptions.grouping !== false) {
      z = 0;
    }
    series.data.forEach((point) => {
      const shapeArgs = point.shapeArgs;
      point.shapeType = 'arc3d';
      const ran = point.options.h / 10;
      shapeArgs.z = z;
      shapeArgs.depth = depth * 0.75 + ran;
      shapeArgs.alpha = alpha;
      shapeArgs.beta = beta;
      shapeArgs.center = series.center;
      shapeArgs.ran = ran;
      const angle = (shapeArgs.end + shapeArgs.start) / 2;
      point.slicedTranslation = {
        translateX: round(cos(angle) * seriesOptions.slicedOffset * cos(alpha * deg2rad)),
        translateY: round(sin(angle) * seriesOptions.slicedOffset * cos(alpha * deg2rad))
      };
    });
  });
  (function (H) {
    H.wrap(Highcharts.SVGRenderer.prototype, 'arc3dPath', function (proceed) {
      // Run original proceed method
      const ret = proceed.apply(this, [].slice.call(arguments, 1));
      ret.zTop = (ret.zOut + 0.5) / 100;
      return ret;
    });
  })(Highcharts);
  return Highcharts.chart(dom, {
    chart: {
      animation: false,
      backgroundColor: 'none',
      type: 'pie', // 饼图
      margin: [0, 0, 0, 0],
      options3d: {
        enabled: true, // 使用3d功能
        alpha: 65, // 延y轴向内的倾斜角度
        beta: 0
      },
      events: {
        load: function () {
          const points = this.series[0].points;
          points.forEach((p) => {
            p.graphic.attr({
              translateY: -p.shapeArgs.ran
            });
            p.graphic.side1.attr({
              translateY: -p.shapeArgs.ran
            });
            p.graphic.side2.attr({
              translateY: -p.shapeArgs.ran
            });
          });
        }
      }
    },
    tooltip: {
      enabled: true, // 禁用悬浮提示框
      formatter: function () {
        if (this.point.name) return this.point.name + ': ' + this.y + '';
        return false;
      }
    },
    legend: {
      enabled: false // 关闭图例
    },
    title: {
      text: ''
    },
    accessibility: {
      enabled: false
    },
    subtitle: {
      text: ''
    },
    plotOptions: {
      pie: {
        allowPointSelect: false, // 禁用点击
        cursor: 'pointer',
        depth: 5,
        showInLegend: true,
        size: 113, // 外圈直径大小
        innerSize: 60, // 内圈直径大小
        center: ['50%', '60%'],
        colors: data.map((item) => item.style.color),
        dataLabels: {
          enabled: false
        }
      }
    },
    credits: {
      enabled: false // 禁用版权信息
    },
    series: [
      {
        type: 'pie',
        name: '数量',
        data: data
      }
    ]
  });
};
